<?php include("session.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style>
/*set a border on the images to prevent shifting*/
#gal1 div { border: 1px solid #000; margin-bottom: 1px; }

 </style>
<title>Sản phẩm</title>
<?php 
    include("import.php");
    include("./service/productService.php");
    include("./service/categoryService.php");
    include("./service/commentService.php");
    include("./service/userService.php");
    include("./service/specialProductService.php");
	include("./util/wideimage/WideImage.php");
?>
<script type="text/javascript">var switchTo5x=true;</script>
 <script type="text/javascript" src="http://w.sharethis.com/button/buttons.js">
 </script>
 <script type="text/javascript">stLight.options({publisher: "1a518bec-0477-40b7-bc2c-547195dfc383", doNotHash: true, doNotCopy: true, hashAddressBar: false});
 </script>
</head>
<?php
    if(isset($_GET["product"])){
        $pro_id = $_GET["product"];    
    }else{
        $pro_id = 0;
    }
    
?>
<body>
<!-- Wrapper -->
<div class="wrapper">
	<!-- Floating banner -->
    <?php include("floating-banner.php");?>
    <!-- End of floating banner -->
    
	<!-- Top banner -->
	<?php include("header.php");?>
    <!-- End of top banner -->
    
    <!-- Navigator -->
	<?php include("navigator.php");?>
    <!-- End of navigator -->
    
    <!-- Menu, newsletter and search -->
    <?php include("menu.php");?>
    <!-- End of menu, newsletter and search -->
    
    <!-- Content -->
        <div class="wrapper-content">
    <?php
        if(!is_null(getProduct($pro_id))){
            $product = getProduct($pro_id);
    ?>
        	<!-- Breadcrumb -->
            <?php include("breadcrumb.php");?>
            <!-- End of breadcrumb -->
            
            <!-- Box content -->
            <div class="box-content-wrapper">
            	<!-- Product box -->
            	<div class="product-box-wrapper">
                	<!-- Left box -->
                	<div class="left-box-wrapper">
                    <?php
                        $imgs = findLimitedImagesOfProduct($pro_id,6);
                        $runner = 1;
                        $no_image = mysql_num_rows($imgs) <= 0;
                        if($no_image){
                    ?>
                        <img style="border:1px solid black;padding:2px;" width="300px" id="zoom" src="./image/product/no-image.jpg" data-zoom-image="./image/product/no-image.jpg"/>
                        <div id="gal1" style="width: 300px; min-height:32px; margin-top: 10px; display:block;padding:3px;">
                            <a href="#" data-image="./image/product/no-image.jpg" data-zoom-image="./image/product/no-image.jpg">
                                <img width="55px" id="zoom" src="./image/product/no-image.jpg" />
                            </a>
                    <?php   
                        }else{
                            while($img = mysql_fetch_array($imgs)){
                    ?>  
                            <?php
                                if($runner == 1){
                            ?>
                                <div class="active-product-wrapper"><img class="active-product" id="zoom" src="./image/product/<?php echo $pro_id."/thumbnail/"; echo $img["IMG_PATH"]; ?>" data-zoom-image="./image/product/<?php echo $pro_id."/"; echo $img["IMG_PATH"]; ?>"/></div>
                            <?php
                                }
                            ?>
                            <?php
                                if($runner == 1){
                            ?>
                                <div id="gal1" style="width: 300px; min-height:32px; margin-top: 5px; display:block; padding: 1px 3px;">
                            <?php
                                }
                            ?> 
                                <div class="inactive-product-wrapper <?php echo ($runner==1) ? "active" : "";?>">
                                    <a href="#" data-image="./image/product/<?php echo $pro_id."/thumbnail/"; echo $img["IMG_PATH"]; ?>" data-zoom-image="./image/product/<?php echo $pro_id."/"; echo $img["IMG_PATH"]; ?>">
                                        <img class="inactive-product" id="zoom" src="./image/product/<?php echo $pro_id."/thumbnail/"; echo $img["IMG_PATH"]; ?>" />
                                        <?php $runner++; ?>
                                    </a>
                                </div>                                
                    <?php
                            }
                        }
                    ?>
                                </div>
						
                    </div>
                    <!-- End of left box -->
                    
                    <!-- Center box -->
                    <div class="center-box-wrapper">
                    	<span class="product-name"><?php echo $product["PRO_NAME"]; ?></span>
                        <?php
                            function getHierachyAttributesOfCategory($cat_id, &$attrs){
								$temps = getAttributesOfCategory($cat_id);
								while($aRecord = mysql_fetch_array($temps)){
									array_push($attrs,$aRecord);
								}
                                $tmp = getCategory($cat_id);
								$parentId = $tmp["CAT_PARENT_ID"];
								if($parentId != 0){
									getHierachyAttributesOfCategory($parentId,$attrs);
								}
							}
                            $category = getCategory($product["CAT_ID"]);
                            $attrs = array();
							getHierachyAttributesOfCategory($category['CAT_ID'],$attrs);
                            $value = null;
                            foreach($attrs as $attr){
                                echo "<span class='dash'>";
                                echo $attr["ATT_NAME"].":";
                                $value = findAttributeValueBy($category["CAT_ID"],$attr["ATT_ID"],$pro_id);
                                echo $value["AVAL_VALUE"]."<br/>";  
                                echo "</span>";
                            }
                        ?>
                        <div class="optional-price-wrapper">
                        	<div class="optional-price-title">Lựa chọn khác</div>
                            <?php
                                function getHierachyOptionsOfCategory($cat_id, &$opts){
    								$temps = getOptionsOfCategory($cat_id);
    								while($aRecord = mysql_fetch_array($temps)){
    									array_push($opts,$aRecord);
    								}
                                    $tmp = getCategory($cat_id);
    								$parentId = $tmp["CAT_PARENT_ID"];
    								if($parentId != 0){
    								    getHierachyOptionsOfCategory($parentId,$opts);
    								}
    							}
                                $category = getCategory($product["CAT_ID"]);
                                $opts = array();
    							getHierachyOptionsOfCategory($category['CAT_ID'],$opts);
                                $value = null;
                                foreach($opts as $opt){
                                    echo "<div class='optional dash'>";
                                    echo $opt["OPP_NAME"].": ";
                                    #$value = findOptionalPriceBy($category["CAT_ID"],$opt["OPP_ID"],$pro_id);
                                    #echo number_format($value["OPV_VALUE"])."<br/>";
                                    $optionitems = findOptionItemOfOption($opt["OPP_ID"]);
                                    echo "<div style='float:right;'>";
                                        echo "<select name='optionItem'>";
                                            while($item = mysql_fetch_array($optionitems)){
                                                echo "<option value='".$item["OOI_ID"]."'>".$item["OOI_NAME"]."</option>"; 
                                            }
                                        echo "</select>";
                                    echo "</div>";
                                    echo "</div>";
                                }
                            ?>
                        </div>
                        <div style="clear: both;"></div>
                        <div style="float: right;">
							<!--<a href="#" 
							  onclick="
								window.open(
								  'https://www.facebook.com/sharer/sharer.php?u='+encodeURIComponent(location.href), 
								  'facebook-share-dialog', 
								  'width=626,height=436'); 
								return false;">
								<img width="40px" height="auto" src="./image/layout/Facebook-Share-Button.png"/>
							</a>-->
							<span class='st_facebook' style="float: left;" displayText='Facebook'></span>
							<span class='st_twitter' style="float: left;" displayText='Tweet'></span>
							<span class='st_googleplus' style="float: left;" displayText='Google +'></span>
							<span class='st_email' style="float: left;" displayText='Email'></span>
                        </div>
                    </div>
                    <!-- End of center box -->
                    
                    <!-- Right box -->
                    <div class="right-box-center">
                    	<div class="add-to-cart-box-wrapper">
                        	<div class="left-ribbon-wrapper">
            		            <div class="left-ribbon">GIÁ</div>
    		                </div>
                            <?php
                                if(!is_null($product["DIS_ID"])){
                                    $discount = getDiscount($product["DIS_ID"]);
                            ?>
                            <div class="old-price"><?php echo number_format($product["PRO_PRICE"]); ?></div>
                            <script>
                                $(".old-price").html(formatCurrency($(".old-price").html(),2));
                            </script>
                            <div class="discounted-price vn-currency"><?php echo ($product["PRO_PRICE"] - ($product["PRO_PRICE"]*$discount["DIS_VALUE"])); ?></div>
                            <?php
                                }else{
                            ?>
                            <div class="discounted-price vn-currency"><?php echo number_format($product["PRO_PRICE"]); ?></div>    
                            <?php
                                }
                            ?>
                            <script>
                                $(".discounted-price").html(formatCurrency($(".discounted-price").html(),2));
                            </script>
                            <div class="add-to-cart-btn-wrapper">
                            	<div class="big-cart-icon" onclick="fly($('.cart-wrapper'), $('.active-product'), 300, 226);addToCart(<?php echo $pro_id;?>);">MUA HÀNG</div>
                            </div>
                        </div>
                        <div class="extra-info">
                        	<ul>
                            	<li>Giao hàng trong <strong>3-5 ngày</strong></li>
                                <li>Giao hàng toàn quốc</li>
                                <li>Thanh toán khi nhận hàng</li>
                                <li>Trả hàng trong 30 ngày</li>
                                <li>HOTLINE: (08) 7452 5214</li>
                            </ul>
                        </div>
                    </div>
                    <!-- End of right box -->
                </div>
                <!-- End of product box -->
                
                <!-- Product description and comments -->
                <div class="product-description-comment-wrapper">
                        <!-- Tabs -->
                	<div class="tabs-wrapper">
                        <ul id="tabs" class="tabs">
                            <?php
                                    $isSpecialProduct = false;
									/*
                                    if(isSpecialProduct($pro_id)){
                                        $specialProduct = findSpecialProductByNormalProduct($pro_id);
										if(!is_null($specialProduct)) 
											$sproid = $specialProduct["SPRO_ID"];
										else
											$sproid = 0;
                                        $isSpecialProduct = true;
                                    } else {
										$sproid = 0;
									}

                                    if($isSpecialProduct){
                            ?>
                                    <li class="active"><a href="#special-product">SẢN PHẨM ĐẶC BIỆT</a></li>
                                    <?php
                                        if(isset($_SESSION["userid"])){
                                    ?>
                                            <li><a href="#online-auction">ĐẤU GIÁ TRỰC TUYẾN</a></li>
                                    <?php
                                        }
                                    ?>
                            <?php
                                    }
									*/
                            ?>
                                    <li class="<?php echo (!$isSpecialProduct ? "active" : ""); ?>"><a href="#description">CHI TIẾT SẢN PHẨM</a></li>
                                    <li><a href="#comment">NHẬN XÉT SẢN PHẨM</a></li>
                        </ul>
                        <!-- Bid Div -->
                            <?php
                                    if($isSpecialProduct){
                                    echo "<div id='special-product' class='tab-content'>".$specialProduct["SPRO_DESCRIPTION"]."</div>";
                            ?>                        
                                
                                <?php
                                    $totalBids = countAllBidOfSpecialProduct($sproid);
                                ?>
                                            <?php
                                                if(isset($_SESSION["userid"])){
                                            ?>
                                                <div id="online-auction" class="tab-content">
                                                    <label>Gửi thông tin đấu giá</label>
                                                    <textarea name="bidtxt" id="bidtxt" class="comment"></textarea>
                                                    <a href="#" class="send-comment-btn" onclick="bid();">GỬI ĐẤU GIÁ</a>
                                                    <hr/>
                                                    <?php
                                                        $page = 1;
                                                        $record_per_page = 5;
                                                        $start_record = ($page * $record_per_page) - 4;
                                                        $bids = getBidsOfSpecialProduct($sproid,$start_record,$record_per_page);
                                                        ?>  
                                                            <div id="bid_block">
                                                                
                                                            </div>
                                                        <div class="clear"></div>
                                                        <!-- Paging -->
                                                        <?php
                                                            if($totalBids > 0){
                                                        ?>
                                                        <div id="example_paging_bid" class="paging"></div>
                                                        <!--<script type='text/javascript'>
                                                            var options = {
                                                                alignment:"right",
                                                                currentPage: 1,
                                                                totalPages: <?php echo ceil($totalBids / 5); ?>,
                                                                onPageClicked: function(e,originalEvent,type,page){
                                                                    $.ajax({
                                                                        type: "GET",
                                                                        url: "./action/ajax/getBidOfSpecialProduct.php",
                                                                        data: { p: page,pro_id:<?php echo $sproid; ?> }
                                                                    }).done(function( data ) {
                                                                        $("#bid_block").empty();
                                                                        $("#bid_block").html(data);
                                                                    });
                                                                }
                                                            }

                                                            $('#example_paging_bid').bootstrapPaginator(options);
                                                        </script>
                                                        -->
                                                        <!-- End of Paging -->
                                                        <?php
                                                            }
                                                        ?>
                                                </div>
                                            <?php
                                                }
                                            ?>  
                        <?php    
                                }
                        ?>
                        <!-- Product Description div -->
                            <div id="description" class="tab-content">
                                <?php echo $product["PRO_DESCRIPTION"]; ?>
                            </div>
                        <!-- Comment div -->
                        <?php
                             $totalComment = countAllRootCommentOfProduct($product["PRO_ID"]);
                        ?>
                        <div id="comment" class="tab-content">
                            <?php
                                if(isset($_SESSION["userid"])){
                            ?>
                                <label>Viết lời nhận xét sản phẩm</label>
                                <textarea name="commenttxt" id="commenttxt" class="comment"></textarea>
                                <a href="#" class="send-comment-btn" onclick="comment();">GỬI NHẬN XÉT</a>
                                <hr/>
                            <?php
                                }
                            ?>
                            
                            <?php
                            $page = 1;
                            $record_per_page = 5;
                            $start_record = ($page * $record_per_page) - 4;
                                $rootComments = getAllApproveFirstCommentOfProduct($pro_id,$start_record,$record_per_page);
                            ?>  
                                <div id="comment_block">
                                    
                                </div>
                            <div class="clear"></div>
                            <!-- Paging -->
                            <?php
                                if($totalComment > 0){
                            ?>
                            <div id="example_paging" class="paging"></div>
                            <script type='text/javascript'>
                                var options = {
                                    alignment:"right",
                                    currentPage: 1,
                                    totalPages: <?php echo ceil($totalComment / 5); ?>,
                                    onPageClicked: function(e,originalEvent,type,page){
                                        $.ajax({
                                            type: "GET",
                                            url: "./action/ajax/getCommentOfProduct.php",
                                            data: { p: page,pro_id:<?php echo $pro_id; ?> }
                                        }).done(function( data ) {
                                            $("#comment_block").empty();
                                            $("#comment_block").html(data);
                                        });
                                    }
                                }
                        
                                $('#example_paging').bootstrapPaginator(options);
                            </script>
                            <!-- End of Paging -->
                            <?php
                                }
                            ?>
                        </div>
                        <!-- End of comment div -->
                	</div>
    				<!-- End of tabs -->
                </div>			
                <!-- End of description and comments -->
                <div class="clear"></div>
            </div>
            <!-- End of box content -->
            <script>
                $(document).ready(function(){
                    $.ajax({
                        type: "GET",
                        url: "./action/ajax/getCommentOfProduct.php",
                        data: { p: 1,pro_id:<?php echo $pro_id; ?> }
                    }).done(function( data ) {
                        $("#comment_block").html(data);
                    });
                    
                    $.ajax({
                        type: "GET",
                        url: "./action/ajax/getBidOfSpecialProduct.php",
                        data: { p: 1,pro_id:<?php echo isset($sproid)?$sproid:0; ?> }
                    }).done(function( data ) {
                        $("#bid_block").empty();
                        $("#bid_block").html(data);
                    });
                    
                    $.ajax({
            			type: "POST",
            			url: "action/ajax/generateBreadcrumb.php",
            			data: { "p" : <?php echo $pro_id ?>},
            			success: function(data){
            				$(".breadcrumb-wrapper").html(data);
            			}
            		});  
                    
                    $("#zoom").elevateZoom({
                        gallery:'gal1', 
                        cursor: 'pointer', 
                        galleryActiveClass: 'active', 
                        imageCrossfade: true, 
                        loadingIcon: 'http://www.elevateweb.co.uk/spinner.gif', 
                        borderSize: 2
                    });  
                });
            </script>
    <?php
        }    
    ?>
    </div>
    <!-- End of content -->
    
    <!-- Live support -->
        <?php include("live-support.php");?>
    <!-- End of live support -->
    
	<?php include("footer.php");?>
</div>
<!-- End of wrapper -->
    <script>
        $(document).ready(function(){
            // change border of wrapper of selected image
            $(".inactive-product-wrapper img").click(function() {
                $(".inactive-product-wrapper").removeClass("active");
                $(this).parent().parent().addClass("active");
            });
        });
        
        function bid(){
           <?php
                if(isset($_SESSION["userid"])){
            ?>
            content = $("#bidtxt").val();
            pro_id = <?php if(isset($sproid)){echo $sproid;}else{echo "0";} ?>;
                if(content.length > 0){
                    $.ajax({
                        type: "POST",
                        url: "./action/ajax/bidProcess.php",
                        data: { content: content, pro_id:pro_id, user:<?php echo $_SESSION["userid"]; ?> }
                    }).done(function( data ) {
                        alert("Xin cám ơn, chúc bạn thắng cuộc");
                        content = $("#bidtxt").val("");
                        window.location.reload(true);
                    });    
                }else{
                    alert("Vui lòng nhập đầy đủ thông tin để tham gia đấu giá");
                }
            <?php
                }else{
            ?>
                alert("Vui lòng đăng nhập");
            <?php                    
                }
            ?>
            return false; 
        }
        function comment(){
            <?php
                if(isset($_SESSION["userid"])){
            ?>
            content = $("#commenttxt").val();
            pro_id = <?php echo $pro_id; ?>;
            if(content.length > 0){
                $.ajax({
                    type: "POST",
                    url: "./action/ajax/comentProcess.php",
                    data: { content: content, pro_id:pro_id, user:<?php echo $_SESSION["userid"]; ?> }
                }).done(function( data ) {
                    alert("Ý kiến của bạn sẽ hiển thị sau khi được xem xét bởi quản trị. Xin cám ơn");
                    content = $("#commenttxt").val("");
                });    
            }
            <?php
                }else{
            ?>
                    alert("Vui lòng đăng nhập");
            <?php                    
                }
            ?>
            return false;
        }
        
        function reply(comment_id){
            <?php
                if(isset($_SESSION["userid"])){
            ?>
                content = $("#txt_"+comment_id).val();
                pro_id = <?php echo $pro_id; ?>;
                if(content.length > 0){
                    $.ajax({
                        type: "POST",
                        url: "./action/ajax/comentProcess.php",
                        data: { content: content, pro_id:pro_id, user:<?php echo $_SESSION["userid"]; ?>,comment:comment_id }
                    }).done(function( data ) {
                        alert("Ý kiến của bạn sẽ hiển thị sau khi được xem xét bởi quản trị. Xin cám ơn");
                        content = $("#txt_"+comment_id).val("");
                        $('#div_'+comment_id).toggle();
                    });
                }
            <?php
                }else{
            ?>
                alert("Vui lòng đăng nhập");
            <?php                    
                }
            ?>
            return false;
        }
        
        function displayReplyTxtField(comment_id){
            <?php
                if(isset($_SESSION["userid"])){
            ?>
                $('#div_'+comment_id).toggle();
            <?php
                }else{
            ?>
                alert("Vui lòng đăng nhập");
            <?php
                }
            ?>
        }
		
        function addToCart(proId){
            var items = new Array();
            
            $('select[name^=optionItem]').each(function() {
                items.push($(this).val());
            });
            
            $.ajax({
                type: "POST",
                url: "action/ajax/addProductToCartProcess.php",
                data: { "proId" : proId, "quantity" : 1, "optionItem" :  items },
                success: function(data){
                        $("#numPro").html("(" + data + ")");
                }
            });
        }
    </script>
</body>
</html>
